def fib(n: Int) = {

  var prev: BigInt = 0
  var cur: BigInt = 1

  // 反復的再帰関数
  def fibIter(i: Int): BigInt = {
    if (i == n) {
      cur
    } else {
      val v = prev + cur
      prev = cur
      cur = v
      fibIter(i + 1) // 末尾再帰
    }
  }
  fibIter(0)
}

val n = args(0).toInt
println(fib(n))
